use hmac::{Hmac, Mac};
use jwt::SignWithKey;
use sha2::Sha256;
use std::collections::BTreeMap;

//解码token，判断是否相等
pub async fn decode_token(phone: &str, password: &str) -> Result<String, &'static str> {
    let key: Hmac<Sha256> = Hmac::new_from_slice(b"vivi66sixsix").map_err(|_e| "生成密码缺少b'参数")?;
    let mut claims = BTreeMap::new();
    claims.insert(phone, password);
    let token_str = claims.sign_with_key(&key).map_err(|_e| "执行生成密码命令错误")?;
    Ok(token_str.into())
}

pub async fn encode_password(password: &String) -> String{
    let code = password.to_string() + "momosaigao";
    blake3::hash(code.as_bytes()).to_string()
}